package com.have.blog.admin.controller;

import com.have.blog.admin.annotation.operationLogger.OperationLogger;
import com.have.blog.admin.domain.dto.StudyVideoDto;
import com.have.blog.admin.service.VideoService;
import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.GetList;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.core.annotion.avoidRepeatableCommit.AvoidRepeatableCommit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/studyVideo")
@Api(value = "学习视频相关接口", tags = {"学习视频相关接口"})
@Slf4j
public class VideoController {
    @Autowired
    private VideoService studyVideoService;

    @ApiOperation(value = "获取学习视频列表", notes = "获取学习视频列表", response = String.class)
    @PostMapping(value = "/getList")
    public Wrapper<CommonPage> getList(@Validated({GetList.class}) @RequestBody StudyVideoDto studyVideoDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取学习视频列表: {}", studyVideoDto);
        List<StudyVideoDto> pageList = studyVideoService.getPageList(studyVideoDto);
        return Wrapper.success(CommonPage.restPage(pageList));
    }

    @AvoidRepeatableCommit
    @OperationLogger(value = "增加学习视频")
    @ApiOperation(value = "增加学习视频", notes = "增加学习视频", response = String.class)
    @PostMapping("/add")
    public Wrapper add(@Validated({Insert.class}) @RequestBody StudyVideoDto studyVideoDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("增加学习视频: {}", studyVideoDto);
        return studyVideoService.addVideo(studyVideoDto);
    }

    @OperationLogger(value = "编辑学习视频")
    @ApiOperation(value = "编辑学习视频", notes = "编辑学习视频", response = String.class)
    @PostMapping("/edit")
    public Wrapper edit(@Validated({Update.class}) @RequestBody StudyVideoDto studyVideoDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("编辑学习视频: {}", studyVideoDto);
        return studyVideoService.editVideo(studyVideoDto);
    }

    @OperationLogger(value = "删除学习视频")
    @ApiOperation(value = "删除学习视频", notes = "删除学习视频", response = String.class)
    @PostMapping("/deleteBatch")
    public Wrapper deleteBatch(@RequestBody List<Integer> uids) {
        // 参数校验
        log.info("删除学习视频: {}", uids);
        return studyVideoService.deleteBatchVideo(uids);
    }
}
